﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.shop
{
    public partial class shopcategory_edite : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("013001002", "对不起，您没有权限进行编辑");
                YXShop.Common.PromptInfo.Popedom("013001004", "对不起，您没有权限进行编辑");
                if (ChangeHope.WebPage.PageRequest.GetInt("id") > 0)
                {
                    BandInfo(ChangeHope.WebPage.PageRequest.GetInt("id"));
                }
                InitWebControl();
            }
        }

        #region 验证
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "输入类别的名称", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtSort, "请选择类别优先级，数字越大排序越靠后", "isint", "数字", "该项必须为数字类型");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion

        /// <summary>
        /// 保存
        /// </summary>
        protected void Save()
        {
            YXShop.BLL.Shop.ShopCategory bll = new YXShop.BLL.Shop.ShopCategory();
            YXShop.Model.Shop.ShopCategory model = new YXShop.Model.Shop.ShopCategory();
            model.Name = this.txtName.Text.Trim();
            model.Sort = Convert.ToInt32(this.txtSort.Text);
            model.IsUse = Convert.ToInt32(this.rabIsUse.SelectedValue);
            if (ViewState["ID"] != null)
            {
                model.ID = Convert.ToInt32(ViewState["ID"].ToString());
                model.Pid = Convert.ToInt32(ViewState["Pid"]);
                model.AddDate = Convert.ToDateTime(ViewState["AddDate"].ToString());
                model.Path = ViewState["Path"].ToString();
                bll.Amend(model);
                this.ltlMsg.Text = "操作成功，已保存该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
                return;
            }
            else
            {
                model.Pid = 0;
                model.AddDate = DateTime.Now;
                model.Path = " ";
                bll.Add(model);
                this.ltlMsg.Text = "操作成功，已添加该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
        }

        /// <summary>
        /// 显示编辑信息
        /// </summary>
        /// <param name="id"></param>
        protected void BandInfo(int id)
        {
            YXShop.BLL.Shop.ShopCategory bll = new YXShop.BLL.Shop.ShopCategory();
            YXShop.Model.Shop.ShopCategory model = bll.GetModelByID(id);
            this.txtName.Text = model.Name;
            this.txtSort.Text = model.Sort.ToString();
            this.rabIsUse.SelectedValue = model.IsUse.ToString();
            ViewState["ID"] = model.ID.ToString();
            ViewState["AddDate"] = model.AddDate.ToString();
            ViewState["Path"] = model.Path.ToString();
            ViewState["Pid"] = model.Pid.ToString();
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

    }
}
